package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Resource
    private DishMapper dishMapper;

    @Resource
    private DishFlavorMapper dishFlavorMapper;

    @Resource
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        //1.转成Dish
        Dish dish = new Dish();
        // 复制属性
        BeanUtils.copyProperties(dishDTO, dish);
        //2.设置状态为停售
        dish.setStatus(StatusConstant.DISABLE);
        //3.添加Dish到数据表中Dish
        dishMapper.insert(dish);
        // 新增菜品的id
        Long dishId = dish.getId();
        //4.添加菜品的口味，多条记录
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //4.1 判断是否有口味，有则需要遍历并添加口味表
        //if (flavors!=null && flavors.size() > 0) {
        if(!CollectionUtils.isEmpty(flavors)){
            /*for (DishFlavor flavor : flavors) {
                // 设置菜品的id
                flavor.setDishId(dishId);
                // 添加菜品口味
                dishFlavorMapper.insert(flavor);
            }*/
            // 使用批量插入
            // 设置每个口味对应的菜品的id
            flavors.forEach(f->f.setDishId(dishId));
            dishFlavorMapper.insertBatch(flavors);
        }
        //5. 操作了2张表，要加上事务控制
    }

    /**
     * 菜品的分页查询
     *
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO pageQueryDTO) {
        // 使用PageHelper
        //1. 设置分页的参数
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        //2. 紧接着的查询语句会被分页
        Page<DishVO> page = dishMapper.selectPage(pageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        if(!CollectionUtils.isEmpty(ids)) {
            //- 起售中的菜品不能删除
            for (Long dishId : ids) {
                Dish dish = dishMapper.getById(dishId);
                if(dish.getStatus() == StatusConstant.ENABLE){
                    throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
                }
            }
            //- 被套餐关联的菜品不能删除
            List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
            if(!CollectionUtils.isEmpty(setmealIds)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
            //- 删除菜品后，关联的口味数据也需要删除掉
            for (Long id : ids) {
                // 先删除菜品
                dishMapper.deleteById(id);
                // 再删除菜品的口味数据
                dishFlavorMapper.deleteByDishId(id);
            }
        }
    }

    /**
     * 查询菜品与口味信息
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdWithFlavors(Long id) {
        //1.通过id查询菜品信息Dish
        Dish dish = dishMapper.getById(id);
        //2.通过菜品的id查询口味信息List<DishFlavors> list
        List<DishFlavor> flavorList = dishFlavorMapper.selectByDishId(id);
        //3.构建Vo对象，设置属性值，返回
        DishVO vo = new DishVO();
        BeanUtils.copyProperties(dish, vo);
        vo.setFlavors(flavorList);
        return vo;
    }

    /**
     * 更新菜品
     *
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        // 更新菜品信息
        dishMapper.update(dish);
        // 删除旧的口味信息
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        // 添加新的口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)) {
            flavors.forEach(f->f.setDishId(dishDTO.getId()));
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}

